#include "controlcommand.h"

ControlCommand::ControlCommand() {}
ControlCommand::~ControlCommand() {}

void ControlCommand::SetCmd(QString cmd)
{
    this->cmd = cmd;
}

void ControlCommand::SetSeatNumber(QString seatnumber)
{
    this->seatnumber = seatnumber;
}

void ControlCommand::SetFileName(QString filename)
{
    this->filename = filename;
}

const QString &ControlCommand::GetCmd() const
{
    return cmd;
}

const QString &ControlCommand::GetSeatNumber() const
{
    return seatnumber;
}

const QString &ControlCommand::GetFileName() const
{
    return filename;
}

ControlCommand &ControlCommand::operator=(ControlCommand &other)
{
    this->SetCmd(other.GetCmd());
    this->SetSeatNumber(other.GetSeatNumber());
    this->SetFileName(other.GetFileName());
    return *this;
}

QString ControlCommand::toJson() const
{
    QJsonObject ccObject;
    ccObject.insert(QStringLiteral("cmd"), cmd);
    ccObject.insert(QStringLiteral("seatnumber"), seatnumber);
    ccObject.insert(QStringLiteral("filename"), filename);
    return QString(QJsonDocument(ccObject).toJson(QJsonDocument::Compact));
}

bool ControlCommand::FromJson(QString jsonStr)
{
    QJsonParseError result;
    QJsonDocument controlCommandObjectRoot = QJsonDocument::fromJson(jsonStr.toUtf8().data(),
                                                                     &result);
    if (result.error != QJsonParseError::NoError) {
        return false;
    }

    QJsonObject controlCommandObject = controlCommandObjectRoot.object();
    SetCmd(controlCommandObject.value("cmd").toString());
    SetSeatNumber(controlCommandObject.value("seatnumber").toString());
    SetFileName(controlCommandObject.value("filename").toString());
    return true;
}

bool ControlCommand::FromJson(QJsonObject jsonObject)
{
    SetCmd(jsonObject.value("cmd").toString());
    SetSeatNumber(jsonObject.value("seatnumber").toString());
    SetFileName(jsonObject.value("filename").toString());
    return true;
}
